package com.mls.guardian.common;

import javafx.scene.shape.Path;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jl on 16/7/5.
 */
public class GitUtil {
    public static String getUpdateShellString(String projectPath, String revisionAddress){
        String str = "";
        if (!Files.isDirectory(Paths.get(projectPath))) {
            str = "git clone " + revisionAddress + " " + projectPath;
        } else {
            //svn update -r 123456 获取制定版本号
            str = "cd "+projectPath+";git pull;ls";
        }

        return str;
    }


    public static String getLastVersion(String projectPath){
        String version = "0";
        try {
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repository = builder.setGitDir(new File(projectPath +"/.git"))
                    .readEnvironment() // scan environment GIT_* variables
                    .findGitDir() // scan up the file system tree
                    .build();


            ObjectId head = repository.resolve("HEAD");

            if(head != null){
                version = head.getName();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return version;
    }
    public static void main(String[] args) throws IOException, GitAPIException {
        String path = "/Users/02-2015-248-pc/gitlab/wms/.git";
        String gitPath = "http://gitlab.mogujie.org/lengqian/trade_wms.git";


        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(new File(path))
                .readEnvironment() // scan environment GIT_* variables
                .findGitDir() // scan up the file system tree
                .build();

        ObjectId head = repository.resolve("HEAD");

        System.out.println(head.getName());

    }
}
